Ext.define('StaffServer.controller.DegreeStudyController', {
	extend : 'Ext.app.Controller',

	models: ['DegreeStudyModel'],
	stores: ['DegreeStudyStore'],
	views:  [
	         'degreeStudy.DegreeStudyMngr', 
	         'degreeStudy.DegreeStudySearchPanel', 
	        
	         'degreeStudy.DegreeStudyDetailGrid', 
	         'degreeStudy.DegreeStudyDetailWindow', 
	         'degreeStudy.DegreeStudyDetailForm',
	         'degreeStudy.DegreeStudyExcelForm', 
	         'degreeStudy.DegreeStudyExcelWindow'],
	
	tabView: 'StaffServer.view.degreeStudy.DegreeStudyMngr',
	
	
	degreeStudyDetailWindow: null,
	submitFlag: null,
	staffRecord: null,
	degreeStudyDetailForm: null,
	degreeStudyExcelForm: null,
	degreeStudyExcelWindow: null,
	
	refs : [ {
		ref : 'degreeStudySearchPanel',
		selector : 'degreeStudySearchPanel'
	}, {
		ref : 'degreeStudyDetailGrid',
		selector : 'degreeStudyDetailGrid'
	}],

	// 初始化执行语句
	init : function() {
		
		degreeStudyDetailWindow = Ext.widget("degreeStudyDetailWindow");
		degreeStudyDetailForm = Ext.ComponentQuery.query('degreeStudyDetailForm')[0];
		degreeStudyExcelWindow = Ext.widget("degreeStudyExcelWindow");
		degreeStudyExcelForm = Ext.ComponentQuery.query('degreeStudyExcelForm')[0];
		// 控制事件
		this.control({
			'degreeStudySearchPanel button[action=search]' : {
				click : this.search
			},
			'degreeStudySearchPanel button[action=reset]' : {
				click : this.reset
			},
		
			'degreeStudyDetailGrid button[action=importExcel]' : {
				click : this.importExcel
			},
			'degreeStudyDetailGrid button[action=exportExcel]' : {
				click : this.exportExcel
			},
			
		
			'degreeStudyDetailGrid button[action=saveDegreeStudy]' : {
				click : this.saveDegreeStudy
			},
			'degreeStudyDetailGrid button[action=updateDegreeStudy]' : {
				click : this.updateDegreeStudy
			},
			'degreeStudyDetailGrid button[action=deleteDegreeStudy]' : {
				click : this.deleteDegreeStudy
			},
			'degreeStudyDetailWindow button[action=submit]' : {
				click : this.submitDetialWindow
			},
			'degreeStudyDetailWindow button[action=close]' : {
				click : this.closeDetailWindow
			},
			'degreeStudyExcelWindow button[action=submit]' : {
				click : this.submitExcelWindow
			},
			'degreeStudyExcelWindow button[action=close]' : {
				click : this.closeExcelWindow
			},
			'degreeStudyDetailGrid': {
				itemdblclick: this.updateDegreeStudy
			}
		});
	},

	//searchPanel查询
	search : function() {
		var searchPanel = this.getDegreeStudySearchPanel();
		var grid = this.getDegreeStudyDetailGrid();
		grid.store.on('beforeload', function(store){
			var searchParams = searchPanel.getForm().getFieldValues();
			Ext.apply(store.proxy.extraParams, searchParams);
        });
		Ext.ComponentQuery.query('degreeStudyDetailGrid > pagingtoolbar')[0].moveFirst();
	},
	
	//searchPanel重置
	reset: function(){
		var searchPanel = this.getDegreeStudySearchPanel();
		searchPanel.getForm().reset();
	},
	
	
	
	
	exportExcel: function(){
		var searchPanel = this.getDegreeStudySearchPanel();
		var searchParams = searchPanel.getForm().getFieldValues();
		var jsonFilter = Ext.JSON.encode(searchParams);
		window.location.href = 'degreeStudy/exportDegreeStudyExcel?jsonFilter=' + jsonFilter;
	},
	
	
	
	
	//新增
	saveDegreeStudy: function(){
		submitFlag = "save";
		degreeStudyDetailWindow.setTitle("新增学历学位进修信息");
		degreeStudyDetailWindow.show();
		
	},
	
	
	//更新
	updateDegreeStudy: function(){
		var record = this.getDegreeStudyDetailGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        degreeStudyDetailForm.loadRecord(record);
        submitFlag = "update";
        degreeStudyDetailWindow.setTitle("修改学历学位进修信息");
        degreeStudyDetailWindow.show();
	},
	
	
	//删除
	deleteDegreeStudy: function(){
		var $this = this;
		var record = this.getDegreeStudyDetailGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        Ext.MessageBox.confirm('请确认', '确认删除吗?', function(btn, text){
            if (btn == 'yes') {
                showWaitMsg(); 
                Ext.Ajax.request({
                    url: 'degreeStudy/deleteDegreeStudy',
                    params: {
                    	id: record.get("id")
                    },
                    success: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        if (resp.success) {
                            Ext.Msg.alert('提示', '操作成功', function(){
                            	$this.getDegreeStudyDetailGrid().getStore().reload();
                            });
                        }
                        else {
                            Ext.Msg.alert('提示', resp.error);
                        }
                    },
                    failure: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        Ext.Msg.alert('提示', resp.error);
                    }
                });
            }
        });
	},
	
	//提交表单
	submitDetialWindow: function(){
		if (!degreeStudyDetailForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = degreeStudyDetailForm.getValues();
		degreeStudyDetailForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		    url: submitFlag == 'save' ? 'degreeStudy/saveDegreeStudy' : 'degreeStudy/updateDegreeStudy',
		    params: {
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeDetailWindow();
		    		$this.getDegreeStudyDetailGrid().getStore().reload();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//关闭窗口
	closeDetailWindow: function(){
		degreeStudyDetailForm.getForm().reset();
		degreeStudyDetailWindow.hide();
	},
	
	
	//导入
	importExcel: function(){
		degreeStudyExcelWindow.setTitle("导入文件");
		degreeStudyExcelWindow.show();
	},
	
	
	//提交表单
	submitExcelWindow: function(){
		if (!degreeStudyExcelForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = degreeStudyExcelForm.getValues();
		degreeStudyExcelForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		    url: 'degreeStudy/importExcel',
		    params: {
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeExcelWindow();
		    		$this.search();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	
	//关闭窗口
	closeExcelWindow: function(){
		degreeStudyExcelForm.getForm().reset();
		degreeStudyExcelWindow.hide();
	}
	
	
});